﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace DemoSailHeroPlugin
{
    /// <summary>
    /// 系统配置操作类
    /// </summary>
    public class ConfigOprater
    {
        /// <summary>
        /// 配置文件的路径
        /// </summary>
        private static string ConfigFilePath = AppDomain.CurrentDomain.BaseDirectory + @"\plugins\" + "DemoSailHeroPlugin.plugin";


        /// <summary>
        /// “Add key=”的操作类
        /// </summary>
        /// <param name="key">key值</param>
        /// <param name="NewValue">需要修改成的新的值</param>
        public static void ModifyKeysValue(string key, string NewValue)
        {

            try
            {
                
                if (!File.Exists(ConfigFilePath)) return;
                XMLConfigUtil utl = new XMLConfigUtil(ConfigFilePath);
                string xpathKey = "//add[@key='{0}']";
                xpathKey = string.Format(xpathKey, key);
                utl.ModifyNodeValue(xpathKey, "value", NewValue);
                utl.Save();
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        /// <summary>
        /// 根据key获取对应的值
        /// </summary>
        /// <param name="key">key值</param>
        public static string GetNodeValue(string key)
        {
            try
            {
                if (!File.Exists(ConfigFilePath)) return null;
                XMLConfigUtil utl = new XMLConfigUtil(ConfigFilePath);
                string xpathKey = "//add[@key='{0}']";
                xpathKey = string.Format(xpathKey, key);
                return utl.GetNodeValue(xpathKey, "value");
            }
            catch (Exception ex)
            {
                throw;
            }
            return null;
        }

    }
}
